import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../../pojo/ImPojo.dart';
import '../../util/DateUtil.dart';
import '../../util/ImConstant.dart';
import '../view/component/UserIconView.dart';
import '../view/component/pane/MessagePane.dart';

class MessageRefInfoPage extends StatelessWidget {
  final ImMessage message;

  const MessageRefInfoPage({required this.message, super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: _buildAppBar(context),
        backgroundColor: ImConstant.IM_MAIN_BACKGROUND_COLOR,
        body: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            _buildDateDiver(),
            _buildRefMessagePane(),
          ],
        ));
  }

  AppBar _buildAppBar(BuildContext context) {
    var title = '';
    if (message.groupCode != null) {
      title = '群聊';
    } else {
      title = message.fromImUser?.userName ?? '';
    }
    return AppBar(
      backgroundColor: ImConstant.IM_MAIN_BACKGROUND_COLOR,
      leading: GestureDetector(
        child: const Icon(Icons.chevron_left, color: Colors.black),
        onTap: () => Navigator.of(context).pop(),
      ),
      centerTitle: true,
      title: Text(
        title,
        style: const TextStyle(color: Colors.black),
      ),
    );
  }

  Widget _buildDateDiver() {
    return Stack(
      alignment: AlignmentDirectional.center,
      children: [
        const Divider(),
        Container(
          color: ImConstant.IM_MAIN_BACKGROUND_COLOR,
          padding: const EdgeInsets.only(left: 10, right: 10),
          child: Text(DateUtil.formatDate(message.createTime) ?? ''),
        )
      ],
    );
  }

  Widget _buildRefMessagePane() {
    return ListTile(
      leading: UserIconView(user: message.fromImUser!, iconSize: 50),
      title: Text(message.fromImUser?.userName??'', style: const TextStyle(fontSize: ImConstant.IM_SMALL_SIZE),),
      subtitle: MessageRefPane(message: message, param: MessageRefParam()),
      trailing: Text(DateUtil.sessionShowTime(message.createTime)??'', style: const TextStyle(fontSize: ImConstant.IM_SMALL_SIZE)),
    );
  }
}
